package cn.com.taurus.controller.user;

import cn.com.taurus.bean.MyPurseWebBean;
import cn.com.taurus.biz.CountClientBiz;
import cn.com.taurus.biz.HomePageWebBeanBiz;
import cn.com.taurus.biz.MyPurseWebBeanBiz;
import cn.com.taurus.entity.*;
import cn.com.taurus.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author yh
 * @createTime 2021年6月25日16:33:18
 * @remark 控制层
 * @version 1.0
 */
@Controller
public class LoginRegisterController {
    @Autowired
    ClientService clientService;
    @Autowired
    ClientAccountService clientAccountService ;
    @Autowired
    ClientAndClientAccountService clientAndClientAccountService;
    @Autowired
    BankService bankService;
    @Autowired
    ClientAccountAndBankService clientAccountAndBankService;
    @Autowired
    MyPurseWebBeanBiz myPurseWebBeanBiz;
    @Autowired
    HomePageWebBeanBiz homePageWebBeanBiz;

    @Autowired
    MyPurseController myPurseController;
    @Autowired
    CountClientBiz countClientBiz;

    @RequestMapping("/Anonymous/register")
    public String register(){
        return "major/openUser";
    }
    @RequestMapping("/Anonymous/clientLoginWeb")
    public String clientLoginWeb(){
        return "major/fundLogin";
    }



    /**
     * 用户对象
     */
    private Client client;
    public Client getClient(){
        return client;
    }
    public void setClient(Client client) {
        this.client = client;
    }

    @RequestMapping("/Anonymous/clientLogin")
    public ModelAndView login(@ModelAttribute Client client , HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        //获取session作用域
        HttpSession session = httpServletRequest.getSession();

        httpServletRequest.setCharacterEncoding("utf-8");
        httpServletResponse.setContentType("text/html;charset=UTF-8"); //解决响应中中文乱码问题OK
        String caccount =httpServletRequest.getParameter("cAccount");
        String cpassword=httpServletRequest.getParameter("password");
        System.out.println(caccount+" "+cpassword);
        Client clientNew = clientService.login(new Client(caccount,cpassword));
        ModelAndView modelAndView = new ModelAndView();
        if (clientNew != null){
            //将Client赋予全局变量
            this.client=clientNew;
            //赋予我的钱包控制层钱包全局对象一个实例
            myPurseController.setMyPurseWebBean(myPurseWebBeanBiz.selectMyPurseWebData(caccount));

            //用户信息等等要放在session作用域中，ModelAndView是request作用域，下次注意点
            //session.setAttribute("myPurseData",myPurseController.getMyPurseWebBean());
            modelAndView.addObject("myPurseData",myPurseController.getMyPurseWebBean());
            modelAndView.addObject("homePageData",homePageWebBeanBiz.selectRandomFiveFund());
            modelAndView.setViewName("major/homepage");
            return modelAndView;
        }else {
            modelAndView.setViewName("major/fundLogin");
            return modelAndView;
        }
    }

    @RequestMapping("/Anonymous/openUser")
    public ModelAndView openUser(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
      /*  httpServletRequest.setCharacterEncoding("utf-8");
        httpServletResponse.setContentType("text/html;charset=UTF-8"); //解决响应中中文乱码问题OK*/
        //客户操作
        Client client =new Client();
        client.setcAccount(httpServletRequest.getParameter("custName"));
        client.setcPassword(httpServletRequest.getParameter("loginPwd"));
        client.setIdCard(httpServletRequest.getParameter("certNo"));
        client.setTel(httpServletRequest.getParameter("mobiles"));
        clientService.clientRegister(client);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        countClientBiz.addCountClient(new CountClinet(0,df.format(new Date()),client.getcId()));
        System.out.println(client.getcId());
        /*******************************/
        //在客户信息表操作
        ClientAccount clientAccount =new ClientAccount(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,"正常");
        clientAccount.setBuyPassword(httpServletRequest.getParameter("reTxPwd"));//交易密码
        clientAccountService.clientAccountRegister(clientAccount);
        System.out.println(clientAccount.getCaId());
        /*******************************/
        //在客户账户信息连接表操作
        ClientAndClientAccount clientAndclientaccount = new ClientAndClientAccount();
        int cid = client.getcId();//获取客户id
        int caid = clientAccount.getCaId();//获取客户账户信息id
        clientAndclientaccount.setCaId(caid);
        clientAndclientaccount.setcId(cid);
        clientAndClientAccountService.clientAndClientAccountRegister(clientAndclientaccount);
        /*******************************/
        //银行卡表操作
        Bank bank =new Bank();
        bankService.bankRegister(bank);
        System.out.println(bank.getbId());
        /*******************************/
        //客户账户信息银行卡连接表操作
        ClientAccountAndBank clientaccountandbank = new ClientAccountAndBank();
        clientaccountandbank.setCaId(caid);//获取客户账户信息id
        int bid = bank.getbId();
        clientaccountandbank.setbId(bid);
        clientAccountAndBankService.clientAccountAndBankRegister(clientaccountandbank);
        return new ModelAndView("major/fundLogin");
    }
}
